package com.hdoit.apollo.web.home.controllers;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.hdoit.apollo.logical.shop.model.Shop;
import com.hdoit.apollo.logical.shop.model.Staff;
import com.hdoit.apollo.logical.shop.service.IShopService;
import com.hdoit.apollo.logical.shop.service.IStaffService;
import com.tennetcn.authority.common.message.AuthorityLoginUser;
import com.tennetcn.authority.common.message.LoginModel;
import com.tennetcn.authority.component.annotation.AuthPassport;
import com.tennetcn.authority.component.web.ActionResult;
import com.tennetcn.authority.component.web.JsonActionResult;
import com.tennetcn.authority.home.controllers.BaseHomeController;
import com.tennetcn.core.common.enums.pub.YesOrNoInteger;
import com.tennetcn.core.common.message.ManageContainer;
import com.tennetcn.core.common.message.ReturnModel;
import com.tennetcn.core.common.utils.CommonUtils;
import com.tennetcn.core.common.utils.DateUtils;
import com.tennetcn.data.enums.ModelStatus;
import com.tennetcn.setting.logical.model.ParamOption;
import com.tennetcn.setting.logical.service.IParamOptionService;

/** 
 * @author      chenghuan
 * @email       79763939@qq.com
 * @createtime  2016年4月14日
 * @comment 
 */
@Controller
@RequestMapping(value = "/admin/home/")
public class HomeController extends BaseHomeController{
	@Override
	public String getRoutePath() {
		return "areas/admin/home/";
	}	
	
	@Autowired
	private IParamOptionService paramOptionService;
	
	@Autowired
	private IStaffService staffService;
	
	@Autowired
	private IShopService shopService;
	
	private final String registerViewName="register";
	private final String loginViewName = "login";
	
	@RequestMapping(value = registerViewName, method = { RequestMethod.GET })
	@AuthPassport
	public String register(Model model) {
		model.addAttribute("title", getTitle());
		return view(registerViewName);
	}
	
	@AuthPassport
	@RequestMapping(value = registerViewName, method = { RequestMethod.POST })
	public String register(Model model,String account,String name,String password,String roleMark) {
		ReturnModel rm=loginUserService.checkAccountRule(account);
		JsonActionResult actionResult=new JsonActionResult();
		
		if(!ManageContainer.SUCCESS.equals(rm.getStatus())) {
			actionResult=new JsonActionResult(ManageContainer.ERROR,rm.getMessage());
		}else if(loginUserService.checkAccount(null, account)){
			actionResult=new JsonActionResult(ManageContainer.ERROR,"已经存在相同账号了");
		}else if(StringUtils.isEmpty(name)){
			actionResult=new JsonActionResult(ManageContainer.ERROR,"姓名不能为空");
		}else if(StringUtils.isEmpty(password)){
			actionResult=new JsonActionResult(ManageContainer.ERROR,"密码不能为空");
		}else {
			Shop shop=new Shop();
			shop.setShopId(CommonUtils.getKeyId());
			shop.setCreateDate(DateUtils.getCurrent());
			shop.setDeleteMark(YesOrNoInteger.no);
			shop.setModelStatus(ModelStatus.add);
			
			roleMark=getRoleMark(roleMark);
			//进行注册调用
			AuthorityLoginUser authorityLoginUser=new AuthorityLoginUser();
			authorityLoginUser.setId(CommonUtils.getKeyId());
			authorityLoginUser.setAccount(account);
			authorityLoginUser.setMobile(account);
			authorityLoginUser.setName(name);
			authorityLoginUser.setPassword(password);
			authorityLoginUser.setRoleMark(roleMark);
			authorityLoginUser.setCreateDate(DateUtils.getCurrent());
			
			staffService.applyStaff(shop,authorityLoginUser);
		}
		model.addAttribute(ManageContainer.RESULTKEY, actionResult);
		if((ManageContainer.ERROR.equals(actionResult.getResultType()))) {
			return view(registerViewName);
		}else {
			return loginViewName;
		}
		
	}
	
	protected String getRoleMark(String roleMark) {
		if(StringUtils.isEmpty(roleMark)) {
			return "boss";
		}
		return roleMark;
	}
	
	@Override
	public String getTitle() {
		ParamOption paramOption=paramOptionService.queryModelByParamName("sys_title");
		if(paramOption!=null) {
			return paramOption.getParamText();
		}else {
			return "订货助手-后台管理系统";
		}
	}
	
	 @Override
	protected boolean logined(LoginModel loginModel,ActionResult actionResult,HttpServletResponse response){
	 System.out.println("realPath:"+servletRequest.getServletContext().getRealPath("/"));
		Staff staff=staffService.queryModel(loginModel.getLoginId());
		System.err.println(loginModel.getLoginId()+"???????");
		if(staff!=null){
			if(staff.getDeleteMark()==0){
				actionResult.setResultType(ManageContainer.ERROR);
				actionResult.setMessage("该员工已离职");
				return false;
			}
		}

		if (!StringUtils.isEmpty(staff)) {
			System.err.println("登陆成功"+staff.getShopId());
		} else {
			System.err.println("登陆失败");
		}
		
		
		
		//webSessionFactory.getCurrentLogin().putArgument("shop", shop);
					
		if(!StringUtils.isEmpty(staff.getShopId())){
			String shopId=staff.getShopId();
					
			Shop shop=shopService.queryModel(shopId);
			loginModel.putArgument("shop", shop);
			loginModel.putArgument( "shopId",shopId);
		}else{
			loginModel.putArgument("shop", null);		
			loginModel.putArgument("shopId", null);	
		}
		
    	return true;
	}
	
}
